﻿using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.LinearGeometry;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.DgnPlatformNET.Elements;
using System.Collections.Generic;
using CheccCoverplateCulvertAssemble.Common;
using CheccCoverplateCulvertAssemble.Conmmon;
using static CheccCoverplateCulvertAssemble.util.DrawLineTool;

namespace CheccCoverplateCulvertAssemble.util
{
    internal class DrawLineTool : DgnElementSetTool
    {
        public delegate (Alignment alignment, double bottomElevation, double angle, double disAlong) GetAlignmentLine();
        GetAlignmentLine _getAlignmentLine;
        // 鼠标选中的第一个点
        DPoint3d firstP;

        // 鼠标选中第二个点
        DPoint3d secondP;

        // 鼠标点击两次构造的直线
        Line getLine = null;

        string flag = null;

        // 转向判断
        string wise = null;

        ElementPropertiesSetter eleSetter = new ElementPropertiesSetter();
        uint color = 5;

        // 引线
        LineElement element = null;

        //Line drawLine = null;

        /// <summary>
        /// 跟随鼠标生成临时的线
        /// </summary>
        //Line tempLine = null;

        public DrawLineTool(GetAlignmentLine getAlignmentLine) : base()
        {
            _getAlignmentLine = getAlignmentLine;
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        protected override void ExitTool()
        {
            if (element != null)
            {
                CommonData.Instance.CommomTotal.SelectLine = element;
            }

            flag = null;
            base.ExitTool();
        }


        protected override void OnPostInstall()
        {
            // System.Windows.Forms.MessageBox.Show("OnPostInstall");
            // base.BeginPickElements();
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            //设置捕捉方式为最近点
            Bentley.MstnPlatformNET.Settings.SnapMode = SnapMode.Nearest;
            // base.OnPostInstall();
            BeginDynamics();
        }


        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);

            if (flag != null)
            {
                //drawLine = tempLine;
                element.AddToModel();
                ExitTool();
                this._getAlignmentLine();
                return true;
            }

            DPoint3d point = ORDHelp.GetMeterPoint(new DPoint3d(ev.Point.X, ev.Point.Y));
            firstP = point;

            flag = "已选位置";

            OnPostInstall();
            return true;
        }

        protected override void OnDynamicFrame(DgnButtonEvent ev)
        {
            if (flag == null) return;

            if (!DynamicsStarted)
            {
                return;
            }

            DPoint3d tempP = ORDHelp.GetMeterPoint(new DPoint3d(ev.Point.X, ev.Point.Y));
            //tempLine = Line.Create1(firstP, tempP);

            //DPoint3d staP = tempLine.StartPoint.Coordinates;
            //DPoint3d endP = tempLine.EndPoint.Coordinates;

            DgnModel model = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
            DSegment3d seg = new DSegment3d(ORDHelp.GetUORPoint(firstP), ORDHelp.GetUORPoint(tempP));
            LineElement lineEle = new LineElement(model, null, seg);
            element = lineEle;

            eleSetter.SetColor(color);
            eleSetter.Apply(element);

            // 设置Element对象line的动态效果
            var redraw = new RedrawElems();
            redraw.DrawMode = DgnDrawMode.TempDraw;
            redraw.DrawPurpose = DrawPurpose.Dynamics;
            redraw.SetDynamicsViewsFromActiveViewSet(ev.Viewport);
            redraw.DoRedraw(element);
        }

        public override Bentley.DgnPlatformNET.StatusInt OnElementModify(Bentley.DgnPlatformNET.Elements.Element element)
        {
            return Bentley.DgnPlatformNET.StatusInt.Error;
        }

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            // 右键后结束动态
            EndDynamics();
            // 结束命令
            ExitTool();
            return true;
        }


        public void InstallNewInstance()
        {
           InstallTool();
        }


    }
}
